/**
 * Displays the XML Code for the test set currently being edited / created. 
 */

FBL.ns(function()
{
    with ( FBL )
    {
        var stateHolder = Components.classes["@mozilla.org/appshell/appShellService;1"].
            getService(Components.interfaces.nsIAppShellService).hiddenDOMWindow;

        var testFire = stateHolder.TestFire;

        function TestFireServerPanel()
        {
        }
        TestFireServerPanel.prototype = extend(Firebug.Panel,
        {
            name: "TestFireServerPanel",
            title: "Server",
            searchable: false,
            editable: false,
            parentPanel: "TestFireTreePanel",


            submitCurrentToServer: function()
            {
                try
                {
                    var httpRequest = new XMLHttpRequest();
                    httpRequest.overrideMimeType('text/xml');
                    var twitch = this;
                    var fcn = function()
                    {
                        if ( httpRequest.readyState == 4 )
                        {
                            if ( httpRequest.status == 200 )
                            {
                                //                               var dialog = window.openDialog("chrome://TestFireExtension/content/dialogs/submitResponseDialog.xul", "SubmiteResponse", "chrome, dialog, modal, resizable=yes,centerscreen", httpRequest.responseText);
                                twitch.setServerResponseText(httpRequest.responseText);
                            }
                            else
                            {
                                twitch.setServerResponseText('There was a problem with the request: '
                                    + httpRequest.responseText);
                            }
                        }
                    };
                    fcn = Firebug.TestFire.util.bindFixed(fcn, this)
                    httpRequest.onreadystatechange = fcn;
                    var str = "&value=" + encodeURIComponent(FirebugContext.getPanel("TestFireCodePanel").asString);
                    var args = "username=" + Firebug.TestFire.settings.username;
                    args += "&password=" + Firebug.TestFire.settings.password;

                    args += str;
                    var url = Firebug.TestFire.settings.testFireServerUrl;
                    url = url + "/ajaxRequest/addNewTestSet.do";
                    httpRequest.open("POST", url, true);
                    httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    httpRequest.send(args);

                    return true;
                }
                catch( ex )
                {
                    Firebug.TestFire.util.tf_debug(ex);
                }
            },


            setServerResponseText: function( text )
            {
                var div = this.panelNode.ownerDocument.getElementById('whereStuffGoes');

                if ( !div )
                {
                    div = this.panelNode.ownerDocument.createElement('div');
                    div.id = 'whereStuffGoes';
                    this.panelNode.appendChild(div);

                }
                while ( div.firstChild )
                {
                    div.removeChild(div.firstChild);
                }
                var ta = document.createElement('textbox');
                ta.id = "theTextContent";
                ta.setAttribute("multiline", true);
                ta.setAttribute("rows", "100");
                ta.setAttribute("cols", "100");
                ta.setAttribute("value", text);

                div.appendChild(ta);

            },



            initializeNode: function( node )
            {
                // test if the page has been built before building it again.
                if ( ! this.panelNode.ownerDocument.getElementById("submitButtonIdXXXXX") )
                {
                    try
                    {
                        while ( this.panelNode.firstChild )
                        {
                            this.panelNode.removeChild(this.panelNode.firstChild);
                        }
                        var doc = document.implementation.createDocument("", "", null);
                        doc.async = false;
                        doc.load("chrome://TestFireExtension/content/dialogs/ServerPanel.xul");
                        this.panelNode.appendChild(doc.getElementById('allContent'));
                        var btn = this.panelNode.ownerDocument.getElementById("submitButtonId");
                        var fcn = Firebug.TestFire.util.bindFixed(this.submitCurrentToServer, this);
                        btn.addEventListener('command', fcn, true);

                    }
                    catch( ex )
                    {
                        Firebug.TestFire.util.tf_debug(ex);
                    }
                }

            },








            showPanel: function( browser, panel )
            {

            },


            refresh: function()
            {
            },



            supportsObject: function( object )
            {
                return false;
            },


            copyTextAreaToClipboard: function()
            {
                var textField = this.panelNode.ownerDocument.getElementById('theTextContent');
                var text = textField.value;
                copyToClipboard(text); // Firebug function.

            },
            _refresh: function()
            {
                this.initializeNode();
            },



            getContextMenuItems: function( node, target )
            {
                try
                {
                    var items = [];
                    items.push(
                        "-",
                    {nol10n:1, label: "Copy To Clipboard", command: bindFixed(this.copyTextAreaToClipboard, this) },
                    {nol10n:1, label: "Refresh", command: Firebug.TestFire.util.bindFixed(this._refresh, this) }
                        );
                }
                catch( ex )
                {
                    Firebug.TestFire.util.tf_debug(ex);
                }

                return items;
            },

            bindFixed: function()
            {
                try
                {
                    var args = FBL.cloneArray(arguments), fn = args.shift(), object = args.shift();
                    return function()
                    {
                        try
                        { return fn.apply(object, FBL.extendArray(args, arguments));}
                        catch( ex )
                        {tf_debug(ex); }
                    }
                }
                catch( ex )
                { Firebug.TestFire.util.tf_debug(ex); }

            }

        }
            )
            ;

        Firebug.registerPanel(TestFireServerPanel);

    }
})
    ;
